﻿using PMIS.Common;
using PMIS.Dialogs;
using PMIS.Events;
using PMIS.Models;
using PMIS.Shared.DTO;
using PMIS.Views;
using System.Collections.ObjectModel;
using System.Windows;

namespace PMIS.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IDialogService _dialogService;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;

        public IRegionManager RegionManager
        {
            get
            {
                return _regionManager;
            }
        }

        public MainWindowViewModel(IDialogService dialogService, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            _dialogService = dialogService;
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;

            //_regionManager.RegisterViewWithRegion("HeaderRegion", typeof(Header));

            _eventAggregator.GetEvent<LoginEvent>().Subscribe(() =>
            {
                LoadNavMenuList();
                this.UserName = "Hello, " + AccountInfo.Instance.UserName;
            });

            _eventAggregator.GetEvent<LoadingEvent>().Subscribe((payload) =>
            {
                this.IsLoading = payload.IsLoading;
                this.LoadingMessage = payload.Message;
            });
        }

        private void LoadNavMenuList()
        {
            var allMenus = AccountInfo.Instance.MenuList;

            //模块/页面
            var navMenuItems = allMenus.Where(x => x.Type == 1 || x.Type == 2).ToList();
            var menuIdList = AccountInfo.Instance.MenuIdList ?? [];

            var menuList = new List<MenuDto>();
            foreach (var item in navMenuItems)
            {
                if (menuIdList.Contains(item.Id))
                {
                    menuList.Add(item);
                }
            }

            if (menuList != null)
            {
                NavItems = new ObservableCollection<MenuDto>(menuList.Where(x => x.ParentId == null));
                foreach (var item in NavItems)
                {
                    LoadChildMenus(item, menuList);
                }
            }
        }

        public void LoadChildMenus(MenuDto menu, List<MenuDto> menuList)
        {
            menu.Children = menuList.Where(x => x.ParentId == menu.Id).ToList();
            foreach (var subitem in menu.Children)
            {
                // Recursive call
                LoadChildMenus(subitem, menuList);
            }
        }

        private ObservableCollection<MenuDto> _navItems;
        public ObservableCollection<MenuDto> NavItems
        {
            get => _navItems;
            set
            {
                SetProperty(ref _navItems, value);
            }
        }

        private DelegateCommand<object> _navCommand;

        public DelegateCommand<object> NavCommand
        {
            get
            {
                if (_navCommand == null)
                {
                    _navCommand = new DelegateCommand<object>(Navigation);
                }
                return _navCommand;
            }
        }

        private DelegateCommand<string> _keyExcuteCommand;

        public DelegateCommand<string> KeyExcuteCommand
        {
            get
            {
                if (_keyExcuteCommand == null)
                {
                    _keyExcuteCommand = new DelegateCommand<string>(KeyExcute);
                }
                return _keyExcuteCommand;
            }
        }

        private bool _isLoading = false;

        public bool IsLoading
        {
            get { return _isLoading; }
            set { SetProperty(ref _isLoading, value); }
        }

        private string _loadingMessage;

        public string LoadingMessage
        {
            get { return _loadingMessage; }
            set { SetProperty(ref _loadingMessage, value); }
        }

        private string _userName;

        public string UserName
        {
            get { return _userName; }
            set { SetProperty(ref _userName, value); }
        }

        private void Navigation(object obj)
        {
            if (obj is MenuDto menu)
            {
                if (!string.IsNullOrEmpty(menu.Url))
                {
                    NavigationParameters parameters = new NavigationParameters();
                    parameters.Add("menu", menu.DeepClone());
                    _regionManager.RequestNavigate("ContentRegion", menu.Url, parameters);
                }
            }
        }

        private void KeyExcute(string s)
        {
            if (s == "Logout")
            {
                Application.Current.MainWindow.Hide();
                _dialogService.ShowDialog(nameof(LoginView), null, (result) =>
                {
                    if (result.Result != ButtonResult.OK)
                    {
                        Application.Current.Shutdown();
                    }
                    else
                    {
                        Application.Current.MainWindow.Show();
                    }
                });
            }
        }
    }
}
